package com.zjc.yohoshop.mvp.view.adapter;

import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.bumptech.glide.Glide;
import com.zjc.yohoshop.bean.ShoesEntity;
import com.zjc.yohoshop.network.API;

import java.util.ArrayList;
import java.util.List;

/**
 * 供应页面顶部轮播
 */
public class ShoesBannerViewPagerAdapter extends PagerAdapter {

    private List<ShoesEntity.BannerBean> list = new ArrayList<>();

    public ShoesBannerViewPagerAdapter(List<ShoesEntity.BannerBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list == null || list.size() == 0){
            return 0;
        }
//        return Integer.MAX_VALUE;
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView img = new ImageView(container.getContext());
        Glide.with(container.getContext()).load(API.BaseUrl + list.get(position % list.size()).recommend_url).into(img);
        container.addView(img);
        return img;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        ImageView img = (ImageView) object;
        container.removeView(img);
    }
}
